package com.hwz.controller.admin;

import com.hwz.dto.CategoryDTO;
import com.hwz.dto.CategoryPageQueryDTO;
import com.hwz.entity.Category;
import com.hwz.result.PageResult;
import com.hwz.result.Result;
import com.hwz.service.CategoryService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Lenovo]
 * @version : [v1.0]
 * @createTime : [2024/3/13 20:54]
 */
@RestController
@Slf4j
@Api(tags = "套餐分类管理")
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    /**
     * 套餐分页查询分类
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("套餐分页查询:{}",categoryPageQueryDTO);
        return Result.success(categoryService.page(categoryPageQueryDTO));
    }

    /**
     * 更新分类
     * @param categoryDTO
     * @return
     */
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("更新分类:{}",categoryDTO);
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 添加分类
     * @param categoryDTO
     * @return
     */
    @PostMapping
    public Result addCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("添加分类:{}",categoryDTO);
        categoryService.addCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 修改分类状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public Result openOrOffCategory(Long id,@PathVariable Integer status){
        log.info("修改分类状态:id={},status={}",id,status);
        categoryService.openOrOffCategory(id,status);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @GetMapping("/list")
    public Result<List<Category>> getCategoryByType(Integer type){
        log.info("根据类型查询分类:{}",type);
        List<Category> categories = categoryService.getByType(type);
        return Result.success(categories);
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public Result deleteCategory(Long id){
        log.info("删除分类:{}",id);
        categoryService.deleteCategory(id);
        return Result.success();
    }
}
